﻿using AngleExpress.utils;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AngleExpress.software
{
    class WeChat : ISoftware
    {
        public override void OpenSoftware(string username, string password, string otherParam)
        {
            string result = ReadWechatPosition(out bool error);
            if (error)
            {
                throw new Exception(result);
            }
            ProcessStartInfo info = new ProcessStartInfo();
            info.UseShellExecute = true;
            info.FileName = result;
            info.Arguments = "";
            try
            {
                Process pro = Process.Start(info);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        string ReadWechatPosition(out bool error)
        {
            error = false;
            RegistryKey Key = Registry.CurrentUser;
            Object regValue;
            try
            {
                RegistryKey wechatReg = Key.OpenSubKey("Software\\Tencent\\WeChat");
                if (wechatReg == null)
                {
                    error = true;
                    return "没有安装微信客户端?";
                }
                regValue = wechatReg.GetValue("InstallPath");
            }
            catch (System.Security.SecurityException)
            {
                error = true;
                return "没有读取注册表权限,请使用管理员账户打开本软件";
            }
            catch (System.UnauthorizedAccessException)
            {
                error = true;
                return "没有读取注册表权限,请使用管理员账户打开本软件";
            }
            if (regValue == null)
            {
                error = true;
                return "找不到注册表项,请联系开发者";
            }
            return regValue.ToString() + "\\WeChat.exe";
        }
    }
}
